# alg.py（最终修正版）
class Solution:
    def longestConsecutive(self, nums):  # 修正方法名
        """
        计算最长连续序列的长度
        """
        if not nums:
            return 0
        
        num_set = set(nums)
        longest_streak = 0
        
        for num in num_set:
            # 检查是否为序列起始点
            if num - 1 not in num_set:
                current_num = num
                current_streak = 1
                
                # 扩展当前序列
                while current_num + 1 in num_set:
                    current_num += 1
                    current_streak += 1
                
                # 更新最长序列
                longest_streak = max(longest_streak, current_streak)
        
        return longest_streak